﻿using Basic.FormModel.QueryColumn;
using Basic.FormRemoteModel;
using LinqKit;
using WeDonekRpc.Helper;

namespace Basic.FormService.Helper
{
    internal static class LinqHelper
    {
        public static ControlType[] ToControlType ( this FormColumnFuncType[] funcType )
        {
            return funcType.IsNull() ? null : funcType.ConvertAll(a =>
            {
                if ( a == FormColumnFuncType.公司ID )
                {
                    return ControlType.Company;
                }
                else if ( a == FormColumnFuncType.部门单位ID )
                {
                    return ControlType.Dept;
                }
                return ControlType.Emp;
            });
        }

        public static FormRecordDataType ToRecordDataType ( this PhyTableDataType type )
        {
            switch ( type )
            {
                case PhyTableDataType.Varchar:
                    return FormRecordDataType.String;
                case PhyTableDataType.NVarchar:
                    return FormRecordDataType.String;
                case PhyTableDataType.Number:
                    return FormRecordDataType.Number;
                case PhyTableDataType.Int:
                    return FormRecordDataType.Long;
                case PhyTableDataType.Date:
                    return FormRecordDataType.Date;
                case PhyTableDataType.DateTime:
                    return FormRecordDataType.DateTime;
                case PhyTableDataType.Short:
                    return FormRecordDataType.Long;
                case PhyTableDataType.Array:
                    return FormRecordDataType.Array;
                case PhyTableDataType.Bool:
                    return FormRecordDataType.Bool;
                case PhyTableDataType.Long:
                    return FormRecordDataType.Long;
                default:
                    return FormRecordDataType.String;
            }
        }
        public static DataFuncType ToDataFuncType ( this FormColumnFuncType type )
        {
            switch ( type )
            {
                case FormColumnFuncType.人员ID:
                    return DataFuncType.人员ID;
                case FormColumnFuncType.部门单位ID:
                    return DataFuncType.部门单位ID;
                case FormColumnFuncType.公司ID:
                    return DataFuncType.公司ID;
                case FormColumnFuncType.字典项:
                    return DataFuncType.字典项;
                case FormColumnFuncType.上传的附件:
                    return DataFuncType.上传的附件;
                case FormColumnFuncType.树形字典:
                    return DataFuncType.树形字典;
                case FormColumnFuncType.选项值:
                    return DataFuncType.选项值;
                default:
                    return DataFuncType.基本;
            }
        }
        public static FormRecordDataType[] GetDataType ( this PhyTableDataType[] dataType )
        {
            if ( dataType.IsNull() )
            {
                return null;
            }
            List<FormRecordDataType> list = new List<FormRecordDataType>();
            dataType.ForEach(c =>
            {
                if ( c == PhyTableDataType.Array )
                {
                    list.Add(FormRecordDataType.Array);
                }
                else if ( c == PhyTableDataType.Short || c == PhyTableDataType.Int || c == PhyTableDataType.Long )
                {
                    list.Add(FormRecordDataType.Long);
                }
                else if ( c == PhyTableDataType.Number )
                {
                    list.Add(FormRecordDataType.Number);
                }
                else if ( c == PhyTableDataType.Bool )
                {
                    list.Add(FormRecordDataType.Bool);
                }
                else if ( c == PhyTableDataType.DateTime )
                {
                    list.Add(FormRecordDataType.DateTime);
                }
                else if ( c == PhyTableDataType.Date )
                {
                    list.Add(FormRecordDataType.Date);
                }
                else
                {
                    list.Add(FormRecordDataType.String);
                }
            });
            return list.Distinct().ToArray();
        }
        public static string GetDataType ( this PhyQueryColDto col )
        {
            if ( col.DataType == PhyTableDataType.Date )
            {
                return "date";
            }
            else if ( col.DataType == PhyTableDataType.DateTime )
            {
                return "smalldatetime";
            }
            else if ( col.DataType == PhyTableDataType.Long )
            {
                return "bigint";
            }
            else if ( col.DataType == PhyTableDataType.Int )
            {
                return "int";
            }
            else if ( col.DataType == PhyTableDataType.Number )
            {
                return "decimal(18," + col.Precision.GetValueOrDefault(0) + ")";
            }
            else if ( col.DataType == PhyTableDataType.Bool )
            {
                return "bit";
            }
            else if ( col.DataType == PhyTableDataType.Short )
            {
                return "smallint";
            }
            else if ( col.DataType == PhyTableDataType.Varchar )
            {
                if ( col.ColLen >= 8000 )
                {
                    return "text";
                }
                else
                {
                    return "varchar(" + col.ColLen + ")";
                }
            }
            else if ( col.DataType == PhyTableDataType.NVarchar )
            {
                if ( col.ColLen >= 8000 )
                {
                    return "ntext";
                }
                else
                {
                    return "nvarchar(" + col.ColLen + ")";
                }
            }
            else
            {
                return "varchar(" + ( col.ColLen == 0 ? "255" : col.ColLen.ToString() ) + ")";
            }
        }
    }
}
